package com.example.medicalapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import androidx.appcompat.app.AppCompatActivity;
import com.example.medicalapp.R;
import com.google.android.material.button.MaterialButton;

// 确保以下两个Activity存在或修改为实际的目标Activity
import com.example.medicalapp.activity.PatientAuthActivity;
import com.example.medicalapp.activity.DoctorAuthActivity;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private MaterialButton patientEntryBtn;
    private MaterialButton doctorEntryBtn;
    private Animation scaleDown;
    private Animation scaleUp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置背景
        getWindow().getDecorView().setBackgroundResource(R.drawable.doctor_background);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate called");

        // 初始化动画（确保anim文件夹下有这两个文件）
        scaleDown = AnimationUtils.loadAnimation(this, R.anim.button_scale);
        scaleUp = AnimationUtils.loadAnimation(this, R.anim.button_scale);

        initViews();
        setButtonListeners();
    }

    private void initViews() {
        // 确保布局文件中存在这两个ID
        patientEntryBtn = findViewById(R.id.patient_entry);
        doctorEntryBtn = findViewById(R.id.doctor_entry);

        // 添加空判断，避免空指针异常
        if (patientEntryBtn == null) {
            Log.e(TAG, "patient_entry button not found in layout");
        }
        if (doctorEntryBtn == null) {
            Log.e(TAG, "doctor_entry button not found in layout");
        }

        Log.d(TAG, "Views initialized");
    }

    private void setButtonListeners() {
        // 检查按钮是否为空再设置监听
        if (patientEntryBtn != null) {
            setupButton(patientEntryBtn, PatientAuthActivity.class, "患者入口");
        }
        if (doctorEntryBtn != null) {
            setupButton(doctorEntryBtn, DoctorAuthActivity.class, "医生入口");
        }
    }

    private void setupButton(MaterialButton button, Class<?> targetActivity, String logTag) {
        button.setOnTouchListener((v, event) -> {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    button.startAnimation(scaleDown);
                    break;
                case MotionEvent.ACTION_UP:
                    button.startAnimation(scaleUp);
                    // 检查目标Activity是否有效
                    if (targetActivity != null) {
                        Intent intent = new Intent(MainActivity.this, targetActivity);
                        startActivity(intent);
                        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                        Log.d(TAG, logTag + " button clicked");
                    } else {
                        Log.e(TAG, logTag + " target activity is null");
                    }
                    break;
            }
            return true; // 修改为true，确保触摸事件被正确处理
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart called");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume called");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause called");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop called");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy called");
    }
}
